#include "parseFile.h"
#include "manhattanCalc.h"

#include <iostream>

parseFile::parseFile(const std::string& nPathFile):
ifs(nPathFile.c_str(), std::ifstream::in){;}

int*	parseFile::readAndMakeIS()
{
	int		count = 0;
	int*	state;	
	
	if (ifs.good())
	{
		ifs >> lenTab; 
		state = new int[lenTab * lenTab];
		while (ifs.good())
			ifs >> state[count++];
		ifs.close();
		return state;
	}
	return NULL;
}

SmartPointer<heuristicCalc>&	parseFile::makeHCalc()
{
	if (lenTab == 3)
	{
		heuristicCalc* calc = new manhattanCalc();
		SmartPointer<heuristicCalc>& ret = SmartPointer<heuristicCalc>( calc );
		return ret;
	}
	return *(SmartPointer<heuristicCalc> *)0;
}

const int	parseFile::getLenTab() const
{
	return lenTab;
}

int*		parseFile::makeFinalState()
{
	int	count = 0;
	int sCount;
	int	n = lenTab;
	int posX = -1, posY = 0;
	int stop = lenTab * lenTab;
	int* fState = new int[stop];
	
	while (count < stop)
	{
		for (sCount = 0; sCount < n; sCount++)
			fState[posY * lenTab + ++posX ] = (++count == stop ? 0 : count);
		n--;
		for (sCount = 0; sCount < n; sCount++)
			fState[++posY * lenTab + posX] = (++count == stop ? 0 : count);
		for (sCount = 0; sCount < n; sCount++)
			fState[posY * lenTab + --posX] = (++count == stop ? 0 : count);
		n--;
		for (sCount = 0; sCount < n; sCount++)
			fState[--posY * lenTab + posX] = (++count == stop ? 0 : count);
	}
	return fState;
}
